package cn.tedu.boot031.controller;

import cn.tedu.boot031.entity.Person;
import cn.tedu.boot031.mapper.PersonMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class PersonController {
    @Autowired
    PersonMapper mapper;

    @RequestMapping("/insert")
    public String insert(Person person){
        mapper.insert(person);
        return "添加成<a href='/'>首页</a>";
    }
    @RequestMapping("/select")
    public String select(){
        List<Person> list = mapper.select();
        String html = "<table border='1'>";
        html+="<caption>员工列表</caption>";
        html +="<tr><th>id</th> <th>员工姓名</th> <th>员工工资</th><th>部门</th><th>操作</th></tr>";
        for (Person p : list) {
         html +="<tr>";
         html +="<td>"+p.getId()+"</td>";
         html +="<td>"+p.getName()+"</td>";
         html +="<td>"+p.getSalary()+"</td>";
         html +="<td>"+p.getDepartment()+"</td>";
         html +="<td><a href='/delete?id="+p.getId()+"'>删除</a></td>";
         html +="</tr>";
        }
        html+="</table>";
        return html;
    }
    @RequestMapping("/delete")
    public String deleteById(int id){
        mapper.deleteById(id);
        return "删除成功<a href='/'>首页</a>";
    }
    @RequestMapping("/update")
    public String update(Person person){
        mapper.update(person);
        return "修改成功 <a href='/'>首页</a>";
    }

}
